package com.woniu108.product.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.dto.ProductInfoDto;
import com.woniu108.carService.commos.dto.UserCartDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.product.form.*;
import com.woniu108.product.param.*;
import com.woniu108.product.service.ProductInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品详情表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/productInfo")
@Api(tags = "商品详情接口")
public class ProductInfoController {

    @Resource
    private ProductInfoService productInfoService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 查询所有商品详情
     * @param pItemId  商品总览id
     * @return 商品详情dto
     */
    @GetMapping("/findAllProductInfo.api")
    @ApiOperation("查询所有商品详情")
    @ApiImplicitParam(name = "pItemId", value = "商品总览id")
    public Result<List<ProductInfoDto>> findAllProductInfo(Integer pItemId, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        FindAllProductInfoParam param = new FindAllProductInfoParam(pItemId, adminId);
        List<ProductInfoDto> dtos = productInfoService.findAllProductInfo(param);
        return Result.newListInstance(ProductInfoDto.class).setData(dtos).setMsg("查询所有商品详情成功");
    }
    /**
     * 查询所有正常商品详情
     * @param pItemId  商品总览id
     * @return 商品详情dto
     */
    @GetMapping("/findAllNormalProductInfo.c")
    @ApiOperation("查询所有正常商品详情")
    public Result<List<ProductInfoDto>> findAllNormalProductInfo(Integer pItemId) {
        List<ProductInfoDto> dtos = productInfoService.findAllNormalProductInfo(pItemId);
        return Result.newListInstance(ProductInfoDto.class).setData(dtos).setMsg("查询所有商品详情总览成功");
    }
    /**
     * 新增商品详情
     * @param form  商品详细名称,商品编号,商品价格,库存,商品描述,商品图片
     * @return boolean值
     * @throws Exception 文件传输异常
     */
    @PostMapping("/insertProductInfo.api")
    @ApiOperation("新增商品详情")@AdminHistoryLog
    public Result<Boolean> insertProductInfo(InsertProductInfoForm form, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        InsertProductInfoParam param = BeanUtil.toBean(form, InsertProductInfoParam.class);
        param.setAdminId(adminId);
        Boolean bool = productInfoService.insertProductInfo(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增商品详情成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增商品详情失败").setData(false).setCode(500);
    }
    /**
     * 修改商品详情
     * @param form 商品详细编号,商品详细名称,商品价格,库存,商品描述
     * @return boolean值
     */
    @PostMapping("/updateProductInfo.api")
    @ApiOperation("修改商品详情")@AdminHistoryLog
    public Result<Boolean> updateProductInfo(@RequestBody UpdateProductInfoForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UpdateProductInfoParam param = BeanUtil.toBean(form, UpdateProductInfoParam.class);
        param.setAdminId(adminId);
        Boolean bool = productInfoService.updateProductInfo(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改商品详情成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改商品详情失败").setData(false).setCode(500);
    }
    /**
     * 修改商品图片
     * @param form 商品详细编号,商品图片
     * @return boolean值
     * @throws Exception 文件传输异常
     */
    @PostMapping("/updateProductPicture.api")
    @ApiOperation("修改商品图片")@AdminHistoryLog
    public Result<Boolean> updateProductPicture(UpdateProductPictureForm form, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UpdateProductPictureParam param = BeanUtil.toBean(form, UpdateProductPictureParam.class);
        param.setAdminId(adminId);
        Boolean bool = productInfoService.updateProductPicture(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改商品图片成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改商品图片失败").setData(false).setCode(500);
    }
    /**
     * 禁用商品详情
     * @param pInfoIds  总览id集合
     * @return boolean值
     */
    @PostMapping("/banProductInfo.api")
    @ApiOperation("禁用商品详情")@AdminHistoryLog
    @ApiImplicitParam(name = "pInfoIds", value = "详情id集合")
    public Result<Boolean> banProductInfo(@RequestBody List<Integer> pInfoIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        BanProductInfoParam param = new BanProductInfoParam(pInfoIds, adminId);
        Boolean bool = productInfoService.banProductInfo(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("禁用商品详情成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("禁用商品详情失败").setData(false).setCode(500);
    }
    /**
     * 解锁商品详情
     * @param pInfoIds  总览id集合
     * @return boolean值
     */
    @PostMapping("/unlockProductInfo.api")
    @ApiOperation("解锁商品详情")@AdminHistoryLog
    @ApiImplicitParam(name = "pInfoIds", value = "详情id集合")
    public Result<Boolean> unlockProductItem(@RequestBody List<Integer> pInfoIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UnlockProductInfoParam param = new UnlockProductInfoParam(pInfoIds, adminId);
        Boolean bool = productInfoService.unlockProductInfo(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("解锁商品详情成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("解锁商品详情失败").setData(false).setCode(500);
    }
    /**
     * 删除商品详情
     * @param pInfoIds  总览id集合
     * @return boolean值
     */
    @PostMapping("/deleteProductInfo.api")
    @ApiOperation("删除商品详情")@AdminHistoryLog
    @ApiImplicitParam(name = "pInfoIds", value = "详情id集合")
    public Result<Boolean> deleteProductInfo(@RequestBody List<Integer> pInfoIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        DeleteProductInfoParam param = new DeleteProductInfoParam(pInfoIds, adminId);
        Boolean bool = productInfoService.deleteProductInfo(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除商品详情成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除商品详情失败").setData(false).setCode(500);
    }

    /**
     *  订单支付成功时商品冻结量减少
     * @param form 商品id，数量
     * @return boolean值
     */
    @PostMapping("/successfulSale")
    @ApiOperation("订单支付成功时商品冻结量减少")
    public Result<Boolean> successfulSale(@RequestBody SuccessfulSaleForm form) {
        Boolean bool = productInfoService.successfulSale(BeanUtil.toBean(form, SuccessfulSaleParam.class));
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("订单支付成功,商品冻结量减少成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("订单支付成功,商品冻结量减少失败").setData(false).setCode(500);
    }
    /**
     *  销售失败时，冻结量调回库存量
     * @param form 商品id，数量
     * @return boolean值
     */
    @PostMapping("/failedSale")
    @ApiOperation("销售失败时，余量增加")
    public Result<Boolean> failedSale(@RequestBody FailedSaleForm form) {
        Boolean bool = productInfoService.failedSale(BeanUtil.toBean(form, FailedSaleParam.class));
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("销售失败时，余量增加").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("销售失败时，余量增加").setData(false).setCode(500);
    }
    /**
     * 减少冻结量
     * @param form 商品id，数量
     * @return  boolean值
     */
    @PostMapping("/submitFreezeNum")
    @ApiOperation("销售失败时，减少冻结量")
    public Result<Boolean> submitFreezeNum(@RequestBody FailedSaleForm form) {
        Boolean bool = productInfoService.submitFreezeNum(BeanUtil.toBean(form, FailedSaleParam.class));
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("销售失败时，减少冻结量").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("销售失败时，减少冻结量").setData(false).setCode(500);
    }
    /**
     * 销量增加
     * @param pInfoId 商品id
     * @return boolean值
     */
    @GetMapping("/addPInfoNum")
    @ApiOperation("销量增加")
    @ApiImplicitParam(name = "pInfoId", value = "详情id")
    public Result<Boolean> addPInfoNum(Integer pInfoId) {
        Boolean bool = productInfoService.addPInfoNum(pInfoId);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("订单支付成功,商品销量增加成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("订单支付成功,商品销量增加失败").setData(false).setCode(500);
    }
    /**
     * 销量减少
     * @param pInfoId 商品id
     * @return boolean值
     */
    @GetMapping("/reducePInfoNum")
    @ApiOperation("销量减少")
    @ApiImplicitParam(name = "pInfoId", value = "详情id")
    public Result<Boolean> reducePInfoNum(Integer pInfoId) {
        Boolean bool = productInfoService.reducePInfoNum(pInfoId);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("退款,商品销量减少成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("退款,商品销量减少失败").setData(false).setCode(500);
    }

    /**
     * 添加商品到购物车
     * @param form 商品id，数量
     * @return boolean值
     * @throws Exception redis异常
     */
    @PostMapping("addGoodsToCart")
    @ApiOperation("添加商品到购物车")
    public Result<Boolean> addGoodsToCart(@RequestBody AddGoodsToCartForm form, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.getString(token);
        String userId = JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        AddGoodsToCartParam param = BeanUtil.toBean(form, AddGoodsToCartParam.class);
        param.setUserId(userId);
        Boolean bool = productInfoService.addGoodsToCart(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("添加商品到购物车成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("添加商品到购物车失败").setData(false).setCode(500);
    }
    /**
     * 修改购物车商品数量
     * @param form 商品id，数量
     * @return boolean值
     * @throws Exception redis异常
     */
    @PostMapping("/updateGoodsNumber")
    @ApiOperation("修改购物车商品数量")
    public Result<Boolean> updateGoodsNumber(@RequestBody UpdateGoodsNumberForm form, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.getString(token);
        String userId = JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        UpdateGoodsNumberParam param = BeanUtil.toBean(form, UpdateGoodsNumberParam.class);
        param.setUserId(userId);
        Boolean bool = productInfoService.updateGoodsNumber(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改购物车商品数量成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改购物车商品数量失败").setData(false).setCode(500);
    }

    /**
     * 删除购物车中某个商品
     * @param pInfoId 商品id
     * @return boolean值
     * @throws Exception redis异常
     */
    @GetMapping("/deleteProductInCart")
    @ApiOperation("删除购物车中某个商品")
    @ApiImplicitParam(name = "pInfoId", value = "商品详情ID")
    public Result<Boolean> deleteProductInCart(Integer pInfoId, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.getString(token);
        String userId = JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        Boolean bool = productInfoService.deleteProductInCart(pInfoId, userId);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除购物车中某个商品成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除购物车中某个商品失败").setData(false).setCode(500);
    }
    /**
     * 查询用户购物车
     * @return 用户购物车商品详情
     */
    @GetMapping("/findUserCart")
    @ApiOperation("查询用户购物车")
    public Result<List<UserCartDto>> findUserCart(@RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String userId = JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        List<UserCartDto> list = productInfoService.findUserCart(userId);
        return Result.newListInstance(UserCartDto.class).setData(list).setMsg("查询用户购物车成功");
    }
    /**
     * 通过ID查询用户购物车
     * @param userId userId
     * @return 用户购物车商品详情
     */
    @GetMapping("/findUserCartByUserId")
    @ApiOperation("通过ID查询用户购物车")
    public Result<List<UserCartDto>> findUserCartByUserId(String userId) {
        List<UserCartDto> list = productInfoService.findUserCart(userId);
        return Result.newListInstance(UserCartDto.class).setData(list).setMsg("查询用户购物车成功");
    }
    /**
     *  下单成功时商品冻结量增加
     * @param form 商品id，数量
     * @return boolean值
     */
    @PostMapping("/successfulOrder")
    @ApiOperation("下单成功，加冻结量，减余量")
    public Result<Boolean> successfulOrder(@RequestBody SuccessfulOrderForm form) {
        Boolean bool = productInfoService.successfulOrder(BeanUtil.toBean(form, SuccessfulOrderParam.class));
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("订单支付成功,商品冻结量减少成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("订单支付成功,商品冻结量减少失败").setData(false).setCode(500);
    }
    /**
     * 通过商品详情ID查商品信息
     * @param pInfoId 商品详情ID
     * @return  商品详情
     */
    @GetMapping("/findProductInfoById")
    @ApiOperation("通过商品详情ID查商品信息")
    public Result<ProductInfoDto> findProductInfoById(Integer pInfoId) {
        ProductInfoDto dto = productInfoService.findProductInfoById(pInfoId);
        return Result.getResult(ProductInfoDto.class).setMsg("通过商品详情ID查商品信息成功").setData(dto);

    }
}

